﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pavilion.DesignerTool
{
    class TrackedResourceBase
    {
        private ResourceItem resourceBase;
        private Dictionary<NodeScript, TrackedNodeScript> trackedNodeScriptCollection;
        private List<NodeScript> originalNodeScript;

        public TrackedResourceBase(ResourceItem resourceBase)
        {
            this.resourceBase = resourceBase;

            trackedNodeScriptCollection = new Dictionary<NodeScript, TrackedNodeScript>();

            if (resourceBase.ContainResourceCollection<NodeScriptCollection>())
            {
                originalNodeScript = new List<NodeScript>(resourceBase.ObtainResourceCollection<NodeScriptCollection>());

                foreach (NodeScript nodeScript in resourceBase.ObtainResourceCollection<NodeScriptCollection>())
                    trackedNodeScriptCollection.Add(nodeScript, new TrackedNodeScript(resourceBase, nodeScript));
            }
            else
                originalNodeScript = new List<NodeScript>();
        }

        public void TransferData()
        {
            CollectionDelta<NodeScript> collectionDelta = new CollectionDelta<NodeScript>(originalNodeScript, resourceBase.ObtainResourceCollection<NodeScriptCollection>());

            foreach (NodeScript deletedNodeScript in collectionDelta.DeletedItems)
                NodeScriptTransfer.Remove(resourceBase.Name, deletedNodeScript);

            foreach (NodeScript newNodeScript in collectionDelta.NewItems)
                NodeScriptTransfer.Add(resourceBase.Name, newNodeScript);

            foreach (NodeScript identicalNodeScript in collectionDelta.IdenticalItems)
                trackedNodeScriptCollection[identicalNodeScript].TransferData();
        }
    }
}
